/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __XOS_STL_POOL_INTERFACE_MAP_H__
#define __XOS_STL_POOL_INTERFACE_MAP_H__

#include "fake_lock.h"
#include <map>

namespace xos_stl
{
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 
	// 直接继承，再加池需要的几个方法
	// 

	template< class KEY, class T, class MapLock = fake_lock >
    class interface_map : public std::map< KEY, T* >
    {
	public:
		typedef MapLock MAP_LOCK;
		typedef interface_map< KEY, T, MAP_LOCK > MAP;
		typedef typename MAP::const_iterator CONST_ITER;
		typedef typename MAP::iterator ITER;
        typedef std::map< KEY, T* > BASE;
	protected:
		MAP_LOCK m_lock;
    public:
        interface_map() : BASE()
		{
		}
        ~interface_map()
		{
			release_all();
		}
	public:
		void for_each_item( f_item_operation<T> fProc )
		{
			for( ITER iter = BASE::begin(); iter != BASE::end(); )
			{
				T * pT = iter++->second;
				fProc( pT );
			}
		}
	public:
		void put_back_to_pool()
		{
			for_each_item( term_pool_item<T> );
			BASE::clear();
		}
		void release_all()
		{
			for_each_item( release_interface<T> );
			BASE::clear();
		}
		void delete_all()
		{
			for_each_item( term_obj<T> );
			BASE::clear();
		}
		void un_lock()
		{
			m_lock.un_lock();
		}
		void lock()
		{
			m_lock.lock();
		}
    };

} // xos_stl

#endif // __XOS_STL_POOL_INTERFACE_MAP_H__
